<?php

namespace app\admin\controller;

use think\Controller;

use think\Db;
use think\Session;
use think\Request;
use app\admin\model\NoticeModel;

//公告/常见问题管理
class Notice extends Common
{	
    //修改状态
    public function up_status()
    { 

       $id=input("get.id");

       $data=Db::table('notice')->where('id',$id)->find();

       $status=$data['status'];

       if($status==1){  //当前启用

          $num=Db::table('notice')->where('id',$id)->update(['status'=>2]);

          if($num>0){

               return 1;

          }else{

               return 2;

          }

      }else{

          $num=Db::table('notice')->where('id',$id)->update(['status'=>1]);

          if($num>0){

               return 3;

          }else{

               return 4;

          }

       }

    }
  //提交列表
	public function index()
  {	  
      return $this->fetch();
  }
  public function list2()
  {   
      return $this->fetch();
  }
  public function lst()
  {	
      	$req=input("post.");
        $get=input('get.');
      	$page=$req['page'];  //当前页   1
        $limit=$req['limit']; //每页几行数据  10
        $begin=($page-1)*$limit;   //0,10  10,10 

        $where=[];
        $where['type']=$get['type'];
        if(isset($req['name']) && !empty($req['name'])){
          $where['title']=['like','%'.$req['name'].'%'];
        }
        
        // if(isset($req['time']) &&!empty($req['time'])){
        //   $time=explode(" ~ ", $req['time']);
        //   $b=strtotime($time[0]);
        //   $e=strtotime($time[1])+3600*24-1;
        //   $where['addtime']=['between',[$b,$e]];
        // }
        // dump($where);
        $count=NoticeModel::where($where)->count();
        $data=[];
        $list=NoticeModel::order('id', 'desc')->where($where)->order('id desc')->paginate($limit,false,['page'=>$page])->all();
        echo json_encode(['code'=>0,'msg'=>'返回成功','count'=>$count,'data'=>$list],320);
  }


  //执行删除
  public function do_del()
  { 
      $data=input("post.");
      $obj=new NoticeModel();
      $num=NoticeModel::destroy($data['id']);
      if($num){
        return ['code'=>1,'msg'=>'删除成功','data'=>''];
      }else{
        return ['code'=>0,'msg'=>'删除失败','data'=>''];
      }
      
    
  }
  //添加
  public function add(){
    if(Request::instance()->isPost()){
          $post=input('post.');
          $get=input('get.');
          $post['type']=$get['type'];
          $post['addtime']=time();
          $notice = new NoticeModel($post);
          $num=$notice->allowField(true)->save();
          if($num){
            return ['code'=>1,'msg'=>'添加成功','data'=>''];
          }else{
            return ['code'=>0,'msg'=>'添加失败','data'=>''];
          }

    }else{
      return $this->fetch();
    }
  }
  //修改
  public function update(){
    $id=input('get.id');
    if(Request::instance()->isPost()){
          $post=input('post.');
          $notice = new NoticeModel();
          $num=$notice->allowField(true)->save($post,['id'=>$id]);
          if($num){
            return ['code'=>1,'msg'=>'添加成功','data'=>''];
          }else{
            return ['code'=>0,'msg'=>'添加失败','data'=>''];
          }

    }else{
      $data=NoticeModel::get($id);
      $this->assign('data',$data);
      return $this->fetch();
    }
  }
  public function detail(){
     $id=input('get.id');
     $data=NoticeModel::get($id);
     $this->assign('data',$data);
     return $this->fetch();
  }

}



